package gzc.xiaonei.action;

import java.util.Map;

import gzc.xiaonei.common.AppConst;
import gzc.xiaonei.model.Users;
import gzc.xiaonei.service.IUserService;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/** 用户登录 */
public class loginAction extends ActionSupport {

	private String email;
	private String pwd;
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Resource
	private IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		if(null != this.getEmail() && null != this.getPwd()){
			System.out.println("Email="+getEmail()+"&Pwd="+getPwd());
			Users user = new Users();
			user.setEmail(this.getEmail());
			user.setPwd(this.getPwd());
			// 查询数据库验证用户是否存在
			user = userService.check(user);
			
			if(null != user){
				//用户合法,必须把该用户bean放入session域对象
				Map session = (Map)ActionContext.getContext().get(AppConst.SESSION_KEY);
				session.put(AppConst.LOGIN_USER_KEY, user);
				return SUCCESS;
			}
		}		
		return ERROR;
	}

}
